#
# Copyright 2013 - Tom Alessi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Models for the Berper Project

   Any models that will be displayed in the DJango admin will have unicode 
   methods to display them properly in the admin

"""


from django.db import models
from django.contrib.auth.models import User


# -- CONFIG RELATED MODELS -- #

class Config(models.Model):
    """Configuration parameters

    """

    config_name = models.CharField(max_length=50, unique=True)
    friendly_name = models.CharField(max_length=50, unique=True)
    config_value = models.CharField(max_length=1000)
    description = models.CharField(max_length=500,blank=False)
    category = models.CharField(max_length=15,blank=False)
    display = models.CharField(max_length=8,blank=False)


class Scan_Runner(models.Model):
    """Scan Runner parameters


    """

    name = models.CharField(max_length=50, unique=True)
    value = models.BooleanField()



# -- SCAN RELATED MODELS -- #

class Scan(models.Model):
    """Targeted Scans"""

    name = models.CharField(max_length=100, unique=True)


class Scan_Type(models.Model):
    """Type of Scan
        - Targeted (targeted = True)
        - Spidered (targeted = False)
    """

    name = models.ForeignKey(Scan)
    targeted = models.BooleanField()


class Scan_Method(models.Model):
    """Method of Scan
        - Active (active = True)
        - Passive (active = False)
    """

    name = models.ForeignKey(Scan)
    active = models.BooleanField()


class Scan_Enabled(models.Model):
    """Status of Scan
        - Enabled (enabled = True)
        - Disabled (enabled = False)
    """

    name = models.ForeignKey(Scan)
    enabled = models.BooleanField()


class Site(models.Model):
    """Target sites"""

    name = models.ForeignKey(Scan)
    site = models.CharField(max_length=200)
    

class Schedule(models.Model):
    """Scan Schedules"""

    name = models.ForeignKey(Scan)
    time = models.TimeField(null=False, blank=False)
    monday = models.BooleanField()
    tuesday = models.BooleanField()
    wednesday = models.BooleanField()
    thursday = models.BooleanField()
    friday = models.BooleanField()
    saturday = models.BooleanField()
    sunday = models.BooleanField()


class Port(models.Model):
    """Scan target ports
        - these can be null, but not both (form validation prevents this)

    """

    name = models.ForeignKey(Scan)
    hport = models.IntegerField(max_length=5, null=True)
    sport = models.IntegerField(max_length=5, null=True)


class URL(models.Model):
    """URLs that will be used for a variety of reason"""

    url = models.CharField(max_length=255, unique=True)


class TScan_List(models.Model):
    """Targeted Scan Lists"""

    name = models.ForeignKey(Scan)
    url = models.ForeignKey(URL)

    # Ensure only one unique URL per scan
    class Meta:
        unique_together = ['name','url']


class Queue(models.Model):
    """Active Scans"""

    name = models.ForeignKey(Scan)
    start = models.DateTimeField(null=False, blank=False)
    report_uid = models.CharField(max_length=36)
    

class Seed_Url(models.Model):
    """Active Scans"""

    name = models.ForeignKey(Scan)
    path = models.CharField(max_length=255)



# -- REPORT RELATED MODELS -- #

class Report(models.Model):
    """Completed Report Locations"""

    name = models.ForeignKey(Scan)
    path = models.CharField(max_length=11)
    report_uid = models.CharField(max_length=36)
    start = models.DateTimeField(null=False, blank=False)
    finish = models.DateTimeField(null=False, blank=False)


class Urls_Scanned(models.Model):
    """Completed Report Locations"""

    report = models.ForeignKey(Report)
    urls = models.IntegerField(max_length=11)


class Requests_Made(models.Model):
    """Requests made during scanning"""

    report = models.ForeignKey(Report)
    requests = models.IntegerField(max_length=11)


class Severity(models.Model):
    """Scan Result Severities"""

    report = models.ForeignKey(Report)
    information = models.IntegerField(max_length=5, null=False)
    low = models.IntegerField(max_length=5, null=False)
    medium = models.IntegerField(max_length=5, null=False)
    high = models.IntegerField(max_length=5, null=False)


class Not_Found(models.Model):
    """URLs that return 404"""

    path = models.CharField(max_length=255, unique=True)


class Not_Found_List(models.Model):
    """Reports and their corresponding 404's"""

    report = models.ForeignKey(Report)
    path = models.ForeignKey(Not_Found)

    # Ensure only one unique path per scan
    class Meta:
        unique_together = ['report','path']